package com.yunyou.modules.wms.basicdata.web;

import com.google.common.collect.Lists;
import com.yunyou.common.config.Global;
import com.yunyou.common.json.AjaxJson;
import com.yunyou.common.utils.time.DateUtils;
import com.yunyou.common.utils.StringUtils;
import com.yunyou.common.utils.excel.ExportExcel;
import com.yunyou.common.utils.excel.ImportExcel;
import com.yunyou.core.persistence.Page;
import com.yunyou.core.web.BaseController;
import com.yunyou.modules.wms.basicdata.entity.BanQinWmCarrierTypeRelation;
import com.yunyou.modules.wms.basicdata.entity.BanQinWmCarrierTypeRelationEntity;
import com.yunyou.modules.wms.basicdata.service.BanQinWmCarrierTypeRelationService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Map;

/**
 * 承运商类型关系Controller
 * @author zyf
 * @version 2020-01-07
 */
@Controller
@RequestMapping(value = "${adminPath}/wms/basicdata/banQinWmCarrierTypeRelation")
public class BanQinWmCarrierTypeRelationController extends BaseController {

	@Autowired
	private BanQinWmCarrierTypeRelationService banQinWmCarrierTypeRelationService;
	
	@ModelAttribute
	public BanQinWmCarrierTypeRelationEntity get(@RequestParam(required=false) String id) {
		BanQinWmCarrierTypeRelationEntity entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = banQinWmCarrierTypeRelationService.getEntity(id);
		}
		if (entity == null){
			entity = new BanQinWmCarrierTypeRelationEntity();
		}
		return entity;
	}
	
	/**
	 * 承运商类型关系列表页面
	 */
	@RequiresPermissions("wms:basicdata:banQinWmCarrierTypeRelation:list")
	@RequestMapping(value = {"list", ""})
	public String list() {
		return "modules/wms/basicdata/banQinWmCarrierTypeRelationList";
	}
	
		/**
	 * 承运商类型关系列表数据
	 */
	@ResponseBody
	@RequiresPermissions("wms:basicdata:banQinWmCarrierTypeRelation:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(BanQinWmCarrierTypeRelationEntity banQinWmCarrierTypeRelationEntity, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BanQinWmCarrierTypeRelationEntity> page = banQinWmCarrierTypeRelationService.findPage(new Page<BanQinWmCarrierTypeRelationEntity>(request, response), banQinWmCarrierTypeRelationEntity);
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加，编辑承运商类型关系表单页面
	 */
	@RequiresPermissions(value={"wms:basicdata:banQinWmCarrierTypeRelation:view","wms:basicdata:banQinWmCarrierTypeRelation:add","wms:basicdata:banQinWmCarrierTypeRelation:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(BanQinWmCarrierTypeRelationEntity banQinWmCarrierTypeRelationEntity, Model model) {
		model.addAttribute("banQinWmCarrierTypeRelationEntity", banQinWmCarrierTypeRelationEntity);
		return "modules/wms/basicdata/banQinWmCarrierTypeRelationForm";
	}

	/**
	 * 保存承运商类型关系
	 */
	@ResponseBody
	@RequiresPermissions(value={"wms:basicdata:banQinWmCarrierTypeRelation:add","wms:basicdata:banQinWmCarrierTypeRelation:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public AjaxJson save(BanQinWmCarrierTypeRelationEntity banQinWmCarrierTypeRelationEntity, Model model, RedirectAttributes redirectAttributes) throws Exception{
		AjaxJson j = new AjaxJson();
		if (!beanValidator(model, banQinWmCarrierTypeRelationEntity)){
			j.setSuccess(false);
			j.setMsg("非法参数！");
			return j;
		}
		try {
			banQinWmCarrierTypeRelationService.save(banQinWmCarrierTypeRelationEntity);//新建或者编辑保存
			j.setSuccess(true);
			j.setMsg("保存承运商类型关系成功");
		} catch (DuplicateKeyException e) {
			j.setSuccess(false);
			j.setMsg("承运商类型关系已维护！");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg(e.getMessage());
		}

		return j;
	}
	
	/**
	 * 删除承运商类型关系
	 */
	@ResponseBody
	@RequiresPermissions("wms:basicdata:banQinWmCarrierTypeRelation:del")
	@RequestMapping(value = "delete")
	public AjaxJson delete(BanQinWmCarrierTypeRelation banQinWmCarrierTypeRelation, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		banQinWmCarrierTypeRelationService.delete(banQinWmCarrierTypeRelation);
		j.setMsg("删除承运商类型关系成功");
		return j;
	}
	
	/**
	 * 批量删除承运商类型关系
	 */
	@ResponseBody
	@RequiresPermissions("wms:basicdata:banQinWmCarrierTypeRelation:del")
	@RequestMapping(value = "deleteAll")
	public AjaxJson deleteAll(String ids, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		String idArray[] =ids.split(",");
		for(String id : idArray){
			banQinWmCarrierTypeRelationService.delete(banQinWmCarrierTypeRelationService.get(id));
		}
		j.setMsg("删除承运商类型关系成功");
		return j;
	}
	
	/**
	 * 导出excel文件
	 */
	@ResponseBody
	@RequiresPermissions("wms:basicdata:banQinWmCarrierTypeRelation:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public AjaxJson exportFile(BanQinWmCarrierTypeRelation banQinWmCarrierTypeRelation, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		try {
            String fileName = "承运商类型关系"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<BanQinWmCarrierTypeRelation> page = banQinWmCarrierTypeRelationService.findPage(new Page<BanQinWmCarrierTypeRelation>(request, response, -1), banQinWmCarrierTypeRelation);
    		new ExportExcel("承运商类型关系", BanQinWmCarrierTypeRelation.class).setDataList(page.getList()).write(response, fileName).dispose();
    		j.setSuccess(true);
    		j.setMsg("导出成功！");
    		return j;
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("导出承运商类型关系记录失败！失败信息："+e.getMessage());
		}
			return j;
    }

	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("wms:basicdata:banQinWmCarrierTypeRelation:import")
    @RequestMapping(value = "import", method=RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<BanQinWmCarrierTypeRelation> list = ei.getDataList(BanQinWmCarrierTypeRelation.class);
			for (BanQinWmCarrierTypeRelation banQinWmCarrierTypeRelation : list){
				try{
					banQinWmCarrierTypeRelationService.save(banQinWmCarrierTypeRelation);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条承运商类型关系记录。");
			}
			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条承运商类型关系记录"+failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入承运商类型关系失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/wms/basicdata/banQinWmCarrierTypeRelation/?repage";
    }
	
	/**
	 * 下载导入承运商类型关系数据模板
	 */
	@RequiresPermissions("wms:basicdata:banQinWmCarrierTypeRelation:import")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "承运商类型关系数据导入模板.xlsx";
    		List<BanQinWmCarrierTypeRelation> list = Lists.newArrayList();
    		new ExportExcel("承运商类型关系数据", BanQinWmCarrierTypeRelation.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/wms/basicdata/banQinWmCarrierTypeRelation/?repage";
    }

}